<template>
  <basic-container>
    <avue-crud v-if="showMain"
               ref="crud"
               v-model="form"
               :before-open="beforeOpen"
               :data="data"
               :option="option"
               :page="page"
               :permission="permissionList"
               :table-loading="loading"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad"
    >
      <template slot="isSign" slot-scope="scope">
        <el-switch v-model="scope.row.isSign" :active-value="1" :inactive-value="0" @change="rowSwitchState(scope.row)"></el-switch>
      </template>

      <template slot="menuLeft">
        <el-button v-if="permission.js_delete"
                   icon="el-icon-delete"
                   plain
                   size="small"
                   type="danger"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menuBtn" slot-scope="{type,size,row,index}">
        <el-dropdown-item divided @click.native="showView(row)">教师风采</el-dropdown-item>
      </template>
      <!--      <template slot-scope="{type,size,row,index}" slot="menu">-->
      <!--        <el-button icon="el-icon-start" :size="size" :type="type" @click="kbVisibled(row)">课表</el-button>-->
      <!--      </template>-->
    </avue-crud>
    <el-dialog :visible.sync="kbVisible" title="课表">
      <div id="coursesTable"></div>
    </el-dialog>
    <JsJsfc v-if="(!showMain)&&show" ref="eps1" :showMainView="showMainView" v-bind:csId="csId"></JsJsfc>
  </basic-container>
</template>

<script>
import {add, getDetail, getList, remove, update} from "@/api/service/js";
import {mapGetters} from "vuex";
// import {getPostList} from "@/api/system/post";
import {getKebiao} from "@/api/service/bjkcsort";
import Timetables from "timetables";
import JsJsfc from "@/views/service/jsJsfc.vue";
import {isMobile} from "@/util/validate";
// import website from "@/config/website";

var timetables = [
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
  ['', '', '', '', '', '', '', '', '', '', '', ''],
];


const highlightWeek = new Date().getDay();

export default {
  components: {
    JsJsfc
  },
  data() {
    return {
      showMain: true,
      show: false,
      csId: '',
      kbVisible: false,
      kb: false,
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        menuType: 'menu',
        menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "姓名",
            prop: "jsName",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入姓名",
              trigger: "blur"
            }],
            maxlength: 50,
            showWordLimit: true
          },
          {
            label: "编号",
            prop: "jsNo",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入编号",
              trigger: "blur"
            }],
            maxlength: 50,
            showWordLimit: true
          },
          {
            label: "学校名称",
            prop: "schoolId",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择学校",
              trigger: "blur"
            }],
            type: "tree",
            dicUrl: "/api/PcPublicService/GetSchoolTreeSelect",
            props: {
              label: "xxName",
              value: "id"
            },
          },
          {
            label: "教师类型",
            prop: "jsType",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择教师类型",
              trigger: "blur"
            }],
            type: 'select',
            dicUrl: "/api/PcPublicService/GetDictionaryList?code=JIAOSHILEIXING",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dataType: "number",
          },
          // {
          //   label: "岗位",
          //   prop: "jsPost",
          //   search: true,
          //   type: "tree",
          //   dicData: [],
          //   overHidden: true,
          //   props: {
          //     label: "postName",
          //     value: "id"
          //   },
          //   rules: [{
          //     required: true,
          //     message: "请选择所属岗位",
          //     trigger: "click"
          //   }],
          // },
          {
            label: "级别",
            prop: "jsLevel",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择级别",
              trigger: "blur"
            }],
            type: 'select',
            dicUrl: "/api/PcPublicService/GetDictionaryList?code=JIAOSHIJIBIE",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dataType: "number",
          },

          {
            label: "性别",
            prop: "jsSex",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择性别",
              trigger: "blur"
            }],
            type: 'select',
            dicUrl: "/api/PcPublicService/GetDictionaryList?code=sex",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dataType: "number",
          },
          {
            label: "出生日期",
            prop: "jsBirth",
            search: true,
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            overHidden: true,
            rules: [{
              required: true,
              message: "请选择出生日期",
              trigger: "blur"
            }]
          },
          {
            label: "电话",
            prop: "jsPhone",
            search: true,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入电话",
              trigger: "blur"
            }],
            maxlength: 11,
            showWordLimit: true
          },
          {
            label: "是否允许登录",
            prop: "isSign",
            type: "radio",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            dicData: [
              {
                label: "否",
                value: 0
              },
              {
                label: "是",
                value: 1
              }
            ],
            // value: 1,
            slot: true,
            // rules: [
            //   {
            //     required: true,
            //     message: "请选择车辆状况",
            //     trigger: "blur"
            //   }
            // ]
          },
          {
            label: "介绍",
            prop: "jsDesc",
            type: "textarea",
            span: 24,
            minRows: 6,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入介绍",
              trigger: "blur"
            }],
            hide: true,
            maxlength: 500,
            showWordLimit: true

          },
          {
            label: "照片",
            prop: "jsPic",
            rules: [{
              required: true,
              message: "请选择照片",
              trigger: "blur"
            }],
            type: 'upload',
            loadText: '附件上传中，请稍等',
            limit: 1,
            listType: 'picture-img',
            span: 24,
            propsHttp: {
              url: 'link',
              name: 'name',
              res: 'data'
            },
            action: '/api/localfiles/uploadPcFile',
            tip: '只能上传jpg/png文件，且不超过500kb',
            hide: true
          },

        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.js_add, false),
        viewBtn: this.vaildData(this.permission.js_view, false),
        delBtn: this.vaildData(this.permission.js_delete, false),
        editBtn: this.vaildData(this.permission.js_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSwitchState(row){
      console.log('row',row)
      console.log('rowBusStatus',row.isSign)
    },
    kbVisibled(row) {
      this.kbVisible = true;
      getKebiao({jsId: row.id}).then(res => {
        timetables = [
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
          ['', '', '', '', '', '', '', '', '', '', '', ''],
        ];
        this.initKebiao(res.data.data);
      });
    },
    initKebiao(list) {
      for (var i in list) {
        var kc = list[i];
        var z = Number(kc.kcZhou) - 1;
        var j = Number(kc.kcJie) - 1;
        var n = kc.kcJieNum;
        timetables[z][j] = kc.kcName;
        if (n > 1) {
          for (var ii = 1; ii < n; ii++) {
            j = j + 1;
            timetables[z][j] = kc.kcName;
          }
        }
      }
      if (this.tt) {
        this.tt.setOption({
          timetables: timetables,
          highlightWeek: highlightWeek,
          merge: false,
          week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
          timetableType: [
            ['上午', 4],
            ['下午', 4],
            ['晚上', 4]

          ],
          // gridOnClick: function (e) {
          //   // console.log(e.name + '  ' + e.week +', 第' + e.index + '节课, 课长' + e.length +'节')
          // },
        })
      } else {
        this.tt = new Timetables({
          el: '#coursesTable',
          timetables: timetables,
          highlightWeek: highlightWeek,
          merge: false,
          week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
          timetableType: [
            ['上午', 4],
            ['下午', 4],
            ['晚上', 4]
          ],
          // gridOnClick: function (e) {
          //   // console.log(e.name + '  ' + e.week +', 第' + e.index + '节课, 课长' + e.length +'节')
          // },
        });
      }


    },
    rowSave(row, done, loading) {
      if (!isMobile(row.jsPhone)) {
        this.$message({
          type: "warning",
          message: "请输入正确手机号!"
        });
        loading();
        return 0
      }
      row.id = row.id === '' ? '0' : row.id
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      if (!isMobile(row.jsPhone)) {
        this.$message({
          type: "warning",
          message: "请输入正确手机号!"
        });
        loading();
        return 0
      }
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: row.id});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove({ids: this.ids});
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      // getPostList(this.form.tenantId).then(res => {
      //   const column = this.findObject(this.option.column, "jsPost");
      //   column.dicData = res.data.data;
      // });
      const school = this.findObject(this.option.column, "schoolId");
      school.hide = true;
    },
    showView(row) {
      this.csId = row.id;
      //this.$refs.eps.onLoad({currentPage:1,pageSize:10});
      this.showMain = false;
      this.show = true;
    },
    showMainView() {
      this.showMain = true;
      this.show = false;
    },
  }
};
</script>

<style>
#coursesTable {
  /*padding: 15px 10px;*/
}

.Courses-head > div {
  text-align: center;
  font-size: 14px;
  line-height: 28px;
}

.left-hand-TextDom, .Courses-head {
  background-color: #f2f6f7;
}

.Courses-leftHand {
  background-color: #f2f6f7;
  font-size: 12px;
}

.Courses-leftHand .left-hand-index {
  color: #9c9c9c;
  margin-bottom: 4px !important;
}

.Courses-leftHand p {
  text-align: center;
  font-weight: 900;
}

.Courses-leftHand > div {
  padding-top: 5px;
  border-bottom: 1px dashed rgb(219, 219, 219);
}

.Courses-leftHand > div:last-child {
  border-bottom: none !important;
}

.left-hand-TextDom, .Courses-head {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1) !important;
}

.Courses-content > ul {
  border-bottom: 1px dashed rgb(219, 219, 219);
  box-sizing: border-box;
}

.Courses-content > ul:last-child {
  border-bottom: none !important;
}

.highlight-week {
  color: #02a9f5 !important;
}

.Courses-content li {
  text-align: center;
  color: #fff;
  font-size: 14px;
  line-height: 50px;
}

.stage_1, .left-hand-1 {
  /*background-color: #FF6666;*/
}

.stage_2, .left-hand-2 {
  /*background-color: #3399CC;*/
}

.stage_3, .left-hand-3 {
  /*background-color: #66CCCC;*/
}

.Courses-leftHand {
  border-right: 1px solid #e0e0e0;
  color: #303133;
}

.Courses-leftHand > div {
  writing-mode: vertical-lr;
  text-align: center;
  line-height: 50px;
  font-size: 16px;
  letter-spacing: 10px;
}
</style>
